﻿using DapperExtensions;
using Nova_DAL;
using Nova_MOD;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nova_BLL
{
    public class TCommandBll
    {
        private TCommandDal dal = new TCommandDal();

        public string InsertEntity(TCommands entity)
        {
            return dal.Insert(entity);
        }

        public string InsertEntity(int taskModel, string trayId, long posS_Id,
                       long posE_Id, int equipmentS_ID, int equipmentE_ID, int commandOrder, string userName)
        {
            TCommands entity = new TCommands();
            entity.TaskMode = taskModel;
            entity.Tray_ID = trayId;
            entity.PosS_ID = posS_Id;
            entity.PosE_ID = posE_Id;
            entity.EquipmentS_ID = equipmentS_ID;
            entity.EquipmentE_ID = equipmentE_ID;
            entity.TaskStatus = 1;
            entity.WriteToPLC = 0;
            entity.SyncWCS = 0;
            entity.OID = userName;
            entity.CommandOrder = commandOrder;
            entity.CDT = DateTime.Now;
            entity.User_Name = userName;
            entity.FLAG_1 = 0;
            return dal.Insert(entity);
        }

        public bool UpdateEntity(TCommands entity)
        {
            return dal.Update(entity);
        }

        public bool DeleteByTaskId(TCommands entity)
        {
            return dal.Delete(entity);
        }

        public TCommands GetEntityByTaskId(long taskId)
        {
            return dal.GetById(taskId.ToString());
        }

        public TCommands GetByTrayId(string trayId)
        {
            var predicate = Predicates.Field<TCommands>(t => t.Tray_ID, Operator.Eq, trayId);
            return dal.GetByField(predicate);
        }

        public DeviceCmdInfoModel GetDeviceCmdInfo()
        {
            return dal.GetDeviceCmdInfo();
        }

        public DeviceCmdInfoModel GetDeviceCmdInfoV()
        {
            var list = dal.GetDeviceCmdList();
            if (list.Count <= 0)
            {
                return null;
            }
            else if (list.Count > 0 && list.Count < 2)
            {
                return list.FirstOrDefault();
            }
            else
            {
                if (list[0].CmdCount > list[1].CmdCount)
                {
                    return list[1];
                }
                else if (list[0].CmdCount < list[1].CmdCount)
                {
                    return list[0];
                }
                else
                {
                    var cmdList = dal.GetListByEquimentE();
                    var sqIdLeftList = cmdList.Where(p => p.EquipmentE_ID == 1001);
                    var sqIdRightList = cmdList.Where(p => p.EquipmentE_ID == 1004);
                    int[] sqIdLeftArray = new int[] {
                        sqIdLeftList.Count(p=>p.TaskMode ==2),
                        sqIdLeftList.Count(p=>p.TaskMode == 1),
                        sqIdLeftList.Count(p=>p.TaskMode == 4)};
                    int[] sqIdRightArray = new int[] {
                        sqIdRightList.Count(p=>p.TaskMode ==2),
                        sqIdRightList.Count(p=>p.TaskMode == 1),
                        sqIdRightList.Count(p=>p.TaskMode == 4)};
                    return Recursion(0, sqIdLeftArray, sqIdRightArray);
                }

            }

        }

        private DeviceCmdInfoModel Recursion(int index, int[] sqIdLeftArray, int[] sqIdRightArray)
        {
            if (index < 3)
            {
                if (sqIdLeftArray[index] > sqIdRightArray[index])
                {
                    return new DeviceCmdInfoModel() { SQID = 1001 };
                }
                else if (sqIdLeftArray[index] < sqIdRightArray[index])
                {
                    return new DeviceCmdInfoModel() { SQID = 1004 };
                }
                else
                {
                    return Recursion((index + 1), sqIdLeftArray, sqIdRightArray);
                }
            }
            else
            {
                return new DeviceCmdInfoModel() { SQID = 1004 };
            }
        }

        public LCDTaskInfoModel GetLcdTaskInfoByTaskId(long taskId)
        {
            return dal.GetLcdTaskInfoByTaskId(taskId);
        }

        public LCDTaskInfoModel GetLcdReplenTaskInfoByTaskId(long taskId)
        {
            return dal.GetLcdReplenTaskInfoByTaskId(taskId);
        }

        public List<TCommands> GetList()
        {
            return dal.GetList();
        }

        public List<TCommands> GetList(int SQID, int taskStatus)
        {
            PredicateGroup predicate = new PredicateGroup()
            {
                Operator = GroupOperator.And,
                Predicates = new List<IPredicate>()
            };
            predicate.Predicates.Add(Predicates.Field<TCommands>(t => t.EquipmentE_ID, Operator.Eq, SQID));
            predicate.Predicates.Add(Predicates.Field<TCommands>(t => t.TaskStatus, Operator.Eq, taskStatus));
            return dal.GetList(predicate);
        }

        public bool IsDeviceEnable(int SQID)
        {
            return dal.IsDeviceEnable(SQID);
        }

        public List<PosPrimitiveTypeModel> GetPosPrimitiveTypeList(int abc_Kind, int typeOfSize)
        {
            return dal.GetPosPrimitiveTypeList(abc_Kind, typeOfSize);
        }
    }
}
